<?php
#等级权益初始化
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';
ini_set('memory_limit', '1024M');

use Biz\model\brandRights\BrandLevelRightsConfig as BrandLevelRightsConfigModel;
use Biz\model\brandRights\BrandLevelRightsSource as BrandLevelRightsSourceModel;
use Biz\model\brandRights\BrandRightsSource as BrandRightsSourceModel;

//权益源
initBrandRightSource();

initLevelRightsSource();


echo "finish" . PHP_EOL;
/**
 * 权益源
 * @return void
 */
function initBrandRightSource()
{
    var_dump('初始化权益源');
    $data = [
        [
            'rights_title' => '用户推荐',
            'rights_name' => 'user_recommend',
            'rights_desc' => '向好好住app新注册用户推荐并引导关注品牌账号，您的品牌账号粉丝数将提高，持续增加品牌曝光。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_OPEN,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 500,
        ],
        [
            'rights_title' => '指数加分',
            'rights_name' => 'index_points',
            'rights_desc' => '品牌指数是基于品牌在好好住运营情况综合计算的得分，直接影响品牌的曝光。达到相应等级时，每日可获得品牌指数额外加分。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 600,
        ],
        [
            'rights_title' => '数据周报',
            'rights_name' => 'data_weekly',
            'rights_desc' => '通过品牌账号系统通知，同步每周运营数据，掌握关键数据驱动运营。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_OPEN,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 700,
        ],
        [
            'rights_title' => '定时发布',
            'rights_name' => 'time_publish',
            'rights_desc' => '在发布内容时，可以根据需要对内容进行「定时发布」设置。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_OPEN,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 800,
        ],
        [
            'rights_title' => '社群推广',
            'rights_name' => 'community_promotion',
            'rights_desc' => '您可以选择一篇优质的住友种草内容，通过在好好住社群的传播，让社群用户被种草、产生决策且提高品牌的知名度。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 900,
        ],
        [
            'rights_title' => '积分商城',
            'rights_name' => 'score_shopping_mall',
            'rights_desc' => '可以不定期参与「积分商城」，让商品获得更多曝光，提升品牌好感度。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_OPEN,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1000,
        ],
        [
            'rights_title' => '定制推广',
            'rights_name' => 'custom_promotion',
            'rights_desc' => '达到对应等级后，可领取好好住社群定制推广权益。当有营销活动或新品发布等重要节点，可以通过好好住社群集中性推广，助力相关活动的传播。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1100,
        ],
        [
            'rights_title' => '去购买展示',
            'rights_name' => 'to_buy_show',
            'rights_desc' => '数据中心可展示近7天商品去购买数',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_OPEN,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1200,
        ],
        [
            'rights_title' => '公众号推广',
            'rights_name' => 'official_accounts_promotion',
            'rights_desc' => '您可以选择一篇优质的住友种草内容，联合好好住公众号进行口碑内容推广。由好好住新媒体负责内容整合，以「口碑好物推荐」的内容形式进行商品推广，让更多用户被种草、产生决策且提高品牌的知名度。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1300,
        ],
        [
            'rights_title' => '内容投放',
            'rights_name' => 'put_content',
            'rights_desc' => '根据选择的投放方式，将优质的内容推荐给目标人群，为内容增加曝光。 可以使用此折扣劵以更低的价格进行投放。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1400,
        ],
        [
            'rights_title' => '内容征集',
            'rights_name' => 'collect_content',
            'rights_desc' => '参与口碑内容征集活动，让更多用户晒图分享品牌好物。帮助品牌提高用户口碑内容量，通过内容传播提升品牌知名度。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1500,
        ],
        [
            'rights_title' => '优惠券发放',
            'rights_name' => 'give_coupon',
            'rights_desc' => '提供一个线上店铺的优惠券链接，在用户福利中心权益展示。用户会前往线上店铺领取，帮助品牌曝光，为品牌带来潜在客源。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1600,
        ],
        [
            'rights_title' => '内容扶持',
            'rights_name' => 'flow_content',
            'rights_desc' => '提供一篇优质的住友口碑内容，通过内容精准投放提高内容转化，让更多用户种草。',
            'rights_type' => BrandRightsSourceModel::RIGHTS_TYPE_LIMIT_NUM,
            'data_status' => 1,
            'price' => 0.00,
            'rights_business_scene' => 1,
            'type' => BrandRightsSourceModel::TYPE_OTHER,
            'sort_weight' => 1700,
        ],
    ];
    foreach ($data as $row) {
        $rightsName = $row['rights_name'];
        $info = BrandRightsSourceModel::getInfo(['rights_name' => $rightsName]);
        if ($info) {
            $isUpdate = false;
            foreach ($row as $key => $value) {
                if ($info[$key] != $value) {
                    $isUpdate = true;
                }
            }
            if ($isUpdate) {
                $res = BrandRightsSourceModel::update($row, $info);
                var_dump('update source rights');
                var_dump(BrandRightsSourceModel::log(), 'rowCount:' . $res->rowCount());
            }
        } else {
            $res = BrandRightsSourceModel::insertData($row);
            var_dump('insert source rights');
            var_dump(BrandRightsSourceModel::log(), 'row id:' . $res);
        }
    }
}


function initLevelRightsSource()
{
    var_dump('初始化等级权益源');
    $levelRights = [
        'user_recommend' =>
            [
                'rights_title' => '用户推荐',
                'rights_desc' => '向好好住app新注册用户推荐并引导关注品牌账号，您的品牌账号粉丝数将提高，持续增加品牌曝光。',
                'sort_weight' => 10,
                'rights_use_pic' => '',
                'lv' => [
                    1 => 0,
                    2 => 0,
                    3 => 0,
                    4 => 0,
                    5 => 0,
                    6 => 0,
                    7 => 0,
                ],
            ],
        'index_points' => [
            'rights_title' => '指数加分',
            'rights_desc' => '品牌指数是基于品牌在好好住运营情况综合计算的得分，直接影响品牌的曝光。达到相应等级时，每日可获得品牌指数额外加分。',
            'sort_weight' => 20,
            'rights_use_pic' => '',
            'lv' => [
                1 => 20,
                2 => 50,
                3 => 100,
                4 => 150,
                5 => 200,
                6 => 300,
                7 => 400,
            ],

        ],
        'blind_box' => [
            'rights_title' => '超级盲盒',
            'rights_desc' => '达到相应等级时，可获得「超级盲盒」免费参与次数。',
            'sort_weight' => 30,
            'rights_use_pic' => '',
            'lv' => [
                1 => 1,
                2 => 1,
                5 => 1,
                7 => 2,
            ],

        ],
        'data_weekly' => [
            'rights_title' => '数据周报',
            'rights_desc' => '通过品牌账号系统通知，同步每周运营数据，掌握关键数据驱动运营。',
            'sort_weight' => 40,
            'rights_use_pic' => '',
            'lv' => [
                2 => 0,
                3 => 0,
                4 => 0,
                5 => 0,
                6 => 0,
                7 => 0,
            ],

        ],
        'time_publish' => [
            'rights_title' => '定时发布',
            'rights_desc' => '在发布内容时，可以根据需要对内容进行「定时发布」设置。',
            'sort_weight' => 50,
            'rights_use_pic' => '',
            'lv' => [
                2 => 0,
                3 => 0,
                4 => 0,
                5 => 0,
                6 => 0,
                7 => 0,
            ],

        ],
        'community_promotion' => [
            'rights_title' => '社群推广',
            'rights_desc' => '您可以选择一篇优质的住友种草内容，通过在好好住社群的传播，让社群用户被种草、产生决策且提高品牌的知名度。',
            'sort_weight' => 60,
            'rights_use_pic' => '',
            'lv' => [
                3 => 1,
                4 => 2,
                5 => 3,
                6 => 4,
                7 => 5,
            ],

        ],
        'new_product' => [
            'rights_title' => '新品速递',
            'rights_desc' => '达到相应等级时，可获得「新品速递」免费参与次数。',
            'sort_weight' => 70,
            'rights_use_pic' => '',
            'lv' => [
                3 => 1,
                5 => 1,
                6 => 1,
                7 => 2,
            ],

        ],
        'content_receive' => [
            'rights_title' => '认领数增加',
            'rights_desc' => '达到相应等级时，可获得更多内容认领篇数。主动认领更多住友内容，可获得更多品牌曝光。',
            'sort_weight' => 80,
            'rights_use_pic' => '',
            'lv' => [
                4 => 100,
                5 => 200,
                6 => 300,
                7 => 99999,
            ],

        ],
        'score_shopping_mall' => [
            'rights_title' => '积分商城',
            'rights_desc' => '可以不定期参与「积分商城」，让商品获得更多曝光，提升品牌好感度。',
            'sort_weight' => 90,
            'rights_use_pic' => '',
            'lv' => [
                4 => 0,
                5 => 0,
                6 => 0,
                7 => 0,
            ],

        ],
        'goods_test_A' => [
            'rights_title' => '好物轻测',
            'rights_desc' => '达到相应等级时，可获得「好物轻测」活动免费参与次数。',
            'sort_weight' => 100,
            'rights_use_pic' => '',
            'lv' => [
                5 => 1,
                6 => 1,
                7 => 2,
            ],
        ],
        'custom_promotion' => [
            'rights_title' => '定制推广',
            'rights_desc' => '达到对应等级后，可领取好好住社群定制推广权益。当有营销活动或新品发布等重要节点，可以通过好好住社群集中性推广，助力相关活动的传播。',
            'sort_weight' => 110,
            'rights_use_pic' => '',
            'lv' => [
                6 => 1,
                7 => 2,
            ],

        ],
        'to_buy_show' => [
            'rights_title' => '去购买展示',
            'rights_desc' => '数据中心可展示近7天商品去购买数。',
            'sort_weight' => 120,
            'rights_use_pic' => '',
            'lv' => [
                7 => 0,
            ],

        ],
        'official_accounts_promotion' => [
            'rights_title' => '公众号推广',
            'rights_desc' => '您可以选择一篇优质的住友种草内容，联合好好住公众号进行口碑内容推广。由好好住新媒体负责内容整合，以「口碑好物推荐」的内容形式进行商品推广，让更多用户被种草、产生决策且提高品牌的知名度。',
            'sort_weight' => 130,
            'rights_use_pic' => '',
            'lv' => [
                7 => 1,
            ],
        ],
        'put_content' => [
            'rights_title' => '内容投放',
            'rights_desc' => '根据选择的投放方式，将优质的内容推荐给目标人群，为内容增加曝光。 可以使用此折扣劵以更低的价格进行投放。',
            'sort_weight' => 140,
            'rights_use_pic' => '',
            'lv' => [
                1 => 1,
                2 => 1,
                3 => 1,
                4 => 1,
                5 => 1,
                6 => 1,
                7 => 1,
            ],
        ],
        'collect_content' => [
            'rights_title' => '内容征集',
            'rights_desc' => '参与口碑内容征集活动，让更多用户晒图分享品牌好物。帮助品牌提高用户口碑内容量，通过内容传播提升品牌知名度。',
            'sort_weight' => 150,
            'rights_use_pic' => '',
            'lv' => [
                3 => 1,
                4 => 1,
                5 => 1,
                6 => 1,
                7 => 1,
            ],
        ],
        'give_coupon' => [
            'rights_title' => '优惠券发放',
            'rights_desc' => '提供一个线上店铺的优惠券链接，在用户福利中心权益展示。用户会前往线上店铺领取，帮助品牌曝光，为品牌带来潜在客源。	',
            'sort_weight' => 160,
            'rights_use_pic' => '',
            'lv' => [
                4 => 1,
                6 => 1,
            ],
        ],
        'flow_content' => [
            'rights_title' => '内容扶持',
            'rights_desc' => '提供一篇优质的住友口碑内容，通过内容精准投放提高内容转化，让更多用户种草。',
            'sort_weight' => 160,
            'rights_use_pic' => '',
            'lv' => [
                2 => 1,
                4 => 1,
                5 => 1,
                6 => 1,
                7 => 1,
            ],
        ]
    ];

    #等级权益源

    $levelRightsData = [];

    foreach ($levelRights as $rightsName => $item) {
        $rightsId = \Biz\service\BrandRights\BrandRightsSourceService::getIdByRightsName($rightsName);
        $baseInfo = [
            'source_rights_id' => $rightsId,
            'data_status' => 1,
        ];
        unset($item['lv']);
        $levelRightsData[] = array_merge($baseInfo, $item);
    }

    foreach ($levelRightsData as $row) {
        //只添加，不做更改
        $info = BrandLevelRightsSourceModel::dataSelectInfo(['source_rights_id' => $rightsId]);
        if (empty($info)) {
            $res = BrandLevelRightsSourceModel::dataInsert($row);
            var_dump('insert level rights source');
            var_dump(BrandLevelRightsSourceModel::log(), 'row id:' . $res);
        }
    }

    sleep(2);

    #等级权益

    $levelRightsCfg = [];

    foreach ($levelRights as $rightsName => $item) {
        $lvs = $item['lv'];
        $rightsId = \Biz\service\BrandRights\BrandRightsSourceService::getIdByRightsName($rightsName);
        foreach ($lvs as $lv => $num) {
            $row = [
                'level' => $lv,
                'rights_source_id' => $rightsId,
                'rights_data' => $num,
                'status' => 1,
                'addtime' => date('Y-m-d H:i:s', time()),
                'edittime' => date('Y-m-d H:i:s', time()),
            ];
            $levelRightsCfg[$lv][] = $row;
        }
    }

    foreach ($levelRightsCfg as $level => $rows) {
        foreach ($rows as $row) {
            $info = BrandLevelRightsConfigModel::dataSelectInfo(['level' => $row['level'], 'rights_source_id' => $row['rights_source_id']]);
            //只添加，不做更改
            if (empty($info)) {
                $res = BrandLevelRightsConfigModel::dataInsert($row);
                var_dump('insert level rights source config');
                var_dump(BrandLevelRightsConfigModel::log(), 'row id:' . $res);
            }
        }
    }
}

